import { getPlaceInfoById,postUserFavoritePlaceDeleteForUser } from '../../api/home'
import { imageURL } from '../../../utils/request'

Page({
  data: {
    startY: 0,
    offsetY: 0,   // 当前偏移
    minY: 0,      // 最小偏移（完全展开）
    maxY: 600,     // 最大偏移（收起位置，自行调整）
    imageURL:'',

    markers: [
      ],
    selectedMarker: null,
    showModal: false,
    collectModal:false,
    showAddPopup:false,
    place_id:'',
    placeDetails:{},
    info_user_favorite_place:'',
    album2:['/pages/assets/image/none-img.png']
  },

  async onLoad(e) {
    this.getLocation();
    this.setData({
      imageURL,
      place_id:e.place_id,
    })
    this.getData()
  },
  async getData(){
    const res = await getPlaceInfoById({
      place_id:this.data.place_id,
      
    })
    this.setData({
      placeDetails:res.data.info,
      markers:[{
        id: this.data.place_id,
        longitude:res.data.info.longitude,
        latitude: res.data.info.latitude,
        iconPath: '/pages/assets/image/detailLocation.svg',
        width: 40,
        height: 40,
        title: res.data.info.name
      }],
      info_user_favorite_place:res.data.info_user_favorite_place,
    })
  },
  onTouchStart(e) {
    this.setData({ startY: e.touches[0].clientY })
  },

  onTouchMove(e) {
    const moveY = e.touches[0].clientY
    let newOffset = this.data.offsetY + (moveY - this.data.startY)
    if (newOffset < this.data.minY) newOffset = this.data.minY
    if (newOffset > this.data.maxY) newOffset = this.data.maxY

    this.setData({
      offsetY: newOffset,
      startY: moveY
    })
  },

  // onTouchEnd() {
  //   // 可选：吸附逻辑（超过一半就展开，否则收起）
  //   const half = (this.data.maxY - this.data.minY) / 2
  //   if (this.data.offsetY > half) {
  //     this.setData({ offsetY: this.data.maxY })
  //   } else {
  //     this.setData({ offsetY: this.data.minY })
  //   }
  // },
  openPopup() {
    if(this.data.info_user_favorite_place){
      this.setData({ 
        collectModal: true,
       });
    }else{
      this.setData({ showAddPopup: true });
    }
  },
  async confirmDelete(){
    const res =await postUserFavoritePlaceDeleteForUser({
      place_id:this.data.place_id
    })
    this.setData({
      collectModal:false
    })
    this.getData()
  },
  onConfirmCollect(e) {
    console.log("選中的收藏夾：", e.detail.folder);
  },
  onPreviewImage(e) {
    const index = e.currentTarget.dataset.index;
    const album = this.data.placeDetails.album || [];
    const urls = album.map(item => this.data.imageURL + item);
    wx.previewImage({
      current: urls[index],
      urls 
    });
  },
  onPreviewImage2() {
    
      wx.showToast({
        title: '暫無圖片可預覽',
        icon: 'none'
      })
  },
  
  // 獲取用戶位置
  getLocation() {
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        // this.setData({
        //   longitude: res.longitude,
        //   latitude: res.latitude,
      
        // });
      },
      fail: () => {
        wx.showToast({ title: '獲取位置失敗', icon: 'none' });
      }
    });
  },

  // 地圖區域變化
  onRegionChange(e) {
    console.log('地圖移動', e);
  },

  // 回到當前位置
  moveToLocation() {
    const mapCtx = wx.createMapContext('myMap');
    mapCtx.moveToLocation();
  },
   // 打開彈框
   openModal() {
    this.setData({ showModal: true });
  },

  // 關閉彈框
  closeModal() {
    this.setData({ 
      showModal: false,
      collectModal: false,
     });
  },

  // 阻止事件穿透（防止遮罩層關閉）
  stopTap() {},

  // 撥打電話
  callPhone() {
    wx.makePhoneCall({
      phoneNumber: this.data.placeDetails.service_hotline
    });
    this.closeModal();
  }

});